package com.dz.lm.web.controller.comp;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.utils.StringUtils;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.domain.comp.Comp;
import com.dz.lm.domain.comprole.CompRole;
import com.dz.lm.service.comp.role.CompRoleService;
import com.dz.lm.web.controller.Basic2Control;
import com.smartlbs.util.DateUtil;
import com.smartlbs.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Map;

@Controller
@RequestMapping("/rewriteUrl/comp/role")
public class CompRoleController extends Basic2Control {
    public Logger logger = LogManager.getLogger(CompRoleController.class);
    @Autowired
    private CompRoleService compRoleService;

    /**
     * 添加部门角色
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/add")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        //角色部门名称
        String name = param.get("name") != null ? param.get("name").toString() : "";
        Integer compId = param.get("compId") != null ? Integer.parseInt(param.get("compId").toString()) : 0;
        //查看
        Integer isLook = param.get("isLook") != null ? Integer.parseInt(param.get("isLook").toString()) : 0;
        //审批
        Integer isReview = param.get("isReview") != null ? Integer.parseInt(param.get("isReview").toString()) : 0;
        //编辑
        Integer isEdit = param.get("isEdit") != null ? Integer.parseInt(param.get("isEdit").toString()) : 0;
        //删除
        Integer isDelete = param.get("isDelete") != null ? Integer.parseInt(param.get("isDelete").toString()) : 0;


        if (StringUtils.isEmpty(name) || compId == 0) {
            message = "参数有误";
        } else {
            CompRole role = new CompRole();
            role.setCompId(compId);
            role.setName(name);
            role.setIsDelete(isDelete);
            role.setIsEdit(isEdit);
            role.setIsLook(isLook);
            role.setIsReview(isReview);
            try {
                role.setCreateTime(DateUtil.getNowDateTimeString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int add = compRoleService.add(role);
            if (add > 0) {
                result = "00";
                message = "添加成功";
            }
        }
        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }

    /**
     * 修改部门角色
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/update")
    public Map update(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        String name = param.get("name") != null ? param.get("name").toString() : "";

        //角色部门id
        Integer roleId = param.get("roleId") != null ? Integer.parseInt(param.get("roleId").toString()) : 0;
        Integer isLook = param.get("isLook") != null ? Integer.parseInt(param.get("isLook").toString()) : 0;
        Integer isReview = param.get("isReview") != null ? Integer.parseInt(param.get("isReview").toString()) : 0;
        Integer isEdit = param.get("isEdit") != null ? Integer.parseInt(param.get("isEdit").toString()) : 0;
        Integer isDelete = param.get("isDelete") != null ? Integer.parseInt(param.get("isDelete").toString()) : 0;


        if (StringUtils.isEmpty(name) || roleId == 0) {
            message = "参数有误";
        } else {
            CompRole role = new CompRole();
            role.setRoleId(roleId);
            role.setName(name);
            role.setIsDelete(isDelete);
            role.setIsEdit(isEdit);
            role.setIsLook(isLook);
            role.setIsReview(isReview);
            try {
                role.setUpdateTime(DateUtil.getNowDateTimeString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int add = compRoleService.update(role);
            if (add > 0) {
                result = "00";
                message = "修改成功";
            }
        }
        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }

    /**
     * 删除
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        String name = param.get("name") != null ? param.get("name").toString() : "";
        //角色部门
        Integer roleId = param.get("roleId") != null ? Integer.parseInt(param.get("roleId").toString()) : 0;
        if (roleId == 0) {
            message = "参数有误";
        } else {

            int add = compRoleService.delete(roleId);
            if (add > 0) {
                result = "00";
                message = "删除成功";
            }
        }
        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }

    /**
     * 获取公司部门列表
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map list(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        String name = param.get("name") != null ? param.get("name").toString() : "";
        //角色部门
        Integer compId = param.get("compId") != null ? StringUtil.getInt(param.get("compId").toString(),0) : 0;
        if (compId == 0) {
            message = "参数有误";
        } else {

            data = compRoleService.getByComp(compId);
            result = "00";
            message = "查询成功";
        }
        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }

}
